$(document).ready(function(){
	// append generate button
	$('<a href=\"javascript:generateLatLng();\" id=\"id_geoGen\"> (click here: auto-generate from FieldAddress)<\/a>').insertAfter( $('#id_fieldLat') );
	
})

function generateLatLng()
{
	addr = $('#id_fieldAddress').val();
	if( addr.length <= 0 )
	{
		$('#id_fieldAddress').val('empty address');
		return false;
	}
	geocoder = new google.maps.Geocoder();
	geocoder.geocode( { address: addr }, function (results, status) {
                        //檢查執行結果
						if( $('#id_geoGenResult').length == 0 ) $('<a id=\"id_geoGenResult\"><\/a>').insertAfter( $('#id_geoGen') );
							
                        if (status == google.maps.GeocoderStatus.OK) {
                            var loc = results[0].geometry.location;
                            $('#id_fieldLat').val(loc.lat());
							$('#id_fieldLng').val(loc.lng());
							$('#id_geoGenResult').text('');
                        }
                        else
                        {
							if( $('#id_geoGenResult').length == 0 )
                            {
								$('<a id=\"id_geoGenResult\"><\/a>').insertAfter( $('#id_geoGen') );
							}
							
							if( status == google.maps.GeocoderStatus.ZERO_RESULTS )
							{
								$('#id_geoGenResult').text(' -(erorr: can\'t get geoCode, please check address is correct)');
							}
							else
							{
								msg = ' -(erorr: functionality fialed, please try another way.)';
								msg += status;
								$('#id_geoGenResult').text(msg);
							}
                        }
                    });
}